<?php

namespace app\admin\model\xilumarket\order;

use app\admin\model\User;
use think\Model;


class Order extends Model
{
    // 表名
    protected $name = 'xilumarket_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'state',
        'state_text'
    ];

    const ORDER_STATUS = [
        '-1'=>'取消',
        '0'=>'待支付',
        '1'=>'待发货',
        '2'=>'待收货',
        '3'=>'待评价',
        '4'=>'已完成',
        '5'=>'部分退款',
        '6'=>'全部退款'
    ];

    //状态对应文本消息
    public function getStateTextAttr($value, $data)
    {
        $state = $this->getAttr('state');
        return self::ORDER_STATUS[$state];
    }

    /**
     * 获取对订单状态
     */
    public function getStateAttr($value, $data)
    {
        if ($data['order_status'] == '3') {
            //已完成
            if($data['refund_status'] == '2'){
                $state = '6';
            }else if($data['refund_status'] == '1'){
                $state = '5';
            }else{
                $state = '4';
            }
        }
        else if ($data['order_status'] == '2') {
            //取消
            $state = '-1';
        }
        else {
            //进行中
            if ($data['pay_status'] == '1') {
                if ($data['freight_status'] == '1') {
                    //已发货
                    if ($data['receipt_status'] == '1') {
                        //已收货
                        if ($data['comment_status'] == '1') {
                            $state = '4';
                        }
                        else {
                            $state = '3';
                        }
                    }
                    else {
                        //待收货
                        $state = '2';
                    }
                }
                else {
                    //待发货
                    $state = '1';
                }
            }
            else {
                //待付款
                $state = '0';
            }
        }
        return $state;
    }
    

    
    public function getPayTypeList()
    {
        return ['1' => __('Pay_type 1'), '2' => __('Pay_type 2'), '3' => __('Pay_type 3')];
    }

    public function getEditAddressStatusList()
    {
        return ['0' => __('Edit_address_status 0'), '1' => __('Edit_address_status 1')];
    }

    public function getFreightTypeList()
    {
        return ['1' => __('Freight_type 1'), '2' => __('Freight_type 2'), '3' => __('Freight_type 3'), '4' => __('Freight_type 4')];
    }

    public function getPayStatusList()
    {
        return ['0' => __('Pay_status 0'), '1' => __('Pay_status 1')];
    }

    public function getFreightStatusList()
    {
        return ['0' => __('Freight_status 0'), '1' => __('Freight_status 1')];
    }

    public function getReceiptStatusList()
    {
        return ['0' => __('Receipt_status 0'), '1' => __('Receipt_status 1')];
    }

    public function getCommentStatusList()
    {
        return ['0' => __('Comment_status 0'), '1' => __('Comment_status 1')];
    }

    public function getRefundStatusList()
    {
        return ['0' => __('Refund_status 0'), '1' => __('Refund_status 1'), '2' => __('Refund_status 2')];
    }

    public function getOrderStatusList()
    {
        return ['1' => __('Order_status 1'), '2' => __('Order_status 2'), '3' => __('Order_status 3')];
    }

    /**
     * 订单用户
     */
    public function user(){
        return $this->belongsTo(User::class,'user_id','id',[],'LEFT')->setEagerlyType(0);
    }
    /**
     * 订单商品
     */
    public function orderProduct(){
        return $this->hasMany(OrderProduct::class,'order_id','id');
    }
    /**
     * 订单用户
     */
    public function seller(){
        return $this->belongsTo(User::class,'seller_user_id','id',[],'LEFT')->setEagerlyType(0);
    }
    /**
     * 订单地址
     */
    public function orderAddress(){
        return $this->hasOne(OrderAddress::class,'order_id','id',[],'LEFT')->setEagerlyType(0);
    }

    /**
     * 订单地址
     */
    public function delivery(){
        return $this->hasOne(OrderDelivery::class,'order_id','id',[],'LEFT')->setEagerlyType(0);
    }
}
